<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="455px" top="10vh" @close='close' class="ruleDialog">
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" :label-width="lang==='zh'?'70px':'110px'" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('名称')" class="ruleTypeForm" prop="name">
          <el-input v-emoji size='small' v-model="record.name" style="width:200px;" ref="recordName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('源')" class="ruleTypeForm" prop="src">
          <el-select v-model="record.src" ref="src" size='small' filterable style="width:200px;">
            <el-option v-for="item in srcdstList" :key="item.name" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('目的')" class="ruleTypeForm" prop="dst">
          <el-select v-model="record.dst" ref="dst" size='small' filterable style="width:200px;">
            <el-option v-for="item in srcdstList" :key="item.name" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('安全引擎')" class="ruleTypeForm" prop="se">
          <el-select v-model="record.se" ref="se" size='small' style="width:200px;">
            <el-option v-for="(value, key) in alertList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('允许Ping')" class="ruleTypeForm" prop="ping">
          <el-select v-model="record.ping" ref="ping" size='small' style="width:200px;">
            <el-option v-for="(value, key) in alertList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('记录日志')" class="ruleTypeForm" prop="log">
          <el-select v-model="record.log" ref="log" size='small' style="width:200px;">
            <el-option v-for="(value, key) in alertList" :key="key" :label="$t(value)" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { appleTemplateInfo } from '@/api/safe/access/template'
import { getAddrGroupArray } from '@/api/list/addr-group'
import { getAddrArray } from '@/api/list/addr'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'AppTemplateDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('请输入名称')))
      } else {
        callback()
      }
    }
    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      record: {
        name: '',
        template: '',
        src: '',
        dst: '',
        se: 'on',
        ping: 'on',
        log: 'on'
      },
      recordCopy: {
        name: '',
        template: '',
        src: '',
        dst: '',
        se: 'on',
        ping: 'on',
        log: 'on'
      },
      alertList: { 'on': this.$t('开'), 'off': this.$t('关') },
      srcdstList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.record.template = data.title
      this.$nextTick(() => {
        const data = ['recordName']
        const selectData = ['src', 'dst', 'se', 'ping', 'log']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
      this.srcdstList = []
      getAddrArray().then(response => {
        if (response.status === 0) {
          getAddrGroupArray().then(res => {
            if (res.status === 0) {
              // this.srcdstList = response.info.concat(res.info)
              for (let item of response.info) {
                if ([1, 2, 3].includes(item.ip_type)) {
                  item.nameShow = this.$t('地址-') + item.name
                  this.srcdstList.push(item)
                }
              }
              for (let item of res.info) {
                item.nameShow = this.$t('地址组-') + item.name
                this.srcdstList.push(item)
              }
              this.record.src = this.srcdstList[0].name
              this.record.dst = this.srcdstList[0].name
            }
          })
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          appleTemplateInfo(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.parentData = {}
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.tip-context {
  color: #999;
  font-size: 12px;
  line-height: 24px;
}
</style>
